package com.evil.formula.api;

import com.evil.common.core.constant.ServiceNameConstant;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import com.evil.formula.config.FeignErrorDecoder;
import com.evil.formula.pojo.dto.CalcReqDTO;
import com.evil.formula.pojo.dto.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.constraints.NotBlank;

/**
 * 公式服务
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@FeignClient(name = ServiceNameConstant.FORMULA_SERVICE, configuration = {FeignErrorDecoder.class})
public interface RemoteFormulaService {

    /**
     * 公式计算
     */
    @PostMapping("/remote/formula/calc")
    Result<?> calc(@Validated @RequestBody CalcReqDTO calcReqDTO);

    default String formula(@Validated CalcReqDTO calcReqDTO) {
        Result<?> result = this.calc(calcReqDTO);
        if (result.succeed()) {
            return result.getData().toString();
        } else {
            throw new BusinessException(RCodeEnum.FORMULA_ERROR, result.getMessage());
        }
    }

    default String formula(@NotBlank(message = "公式不能为空") String formula) {
        CalcReqDTO calcReqDTO = new CalcReqDTO();
        calcReqDTO.setFormula(formula);
        return this.formula(calcReqDTO);
    }
}
